package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference(cluster = "failfast")
    SetmealService setmealService;
    @Autowired
    JedisUtil jedisUtil;

    /**
     * 1、form表单提交数据的格式 （name=jack&age=18&address=shangshou） 后端用对象接收不需要加注解
     * 2、页面传的是json数据，后端使用map 或者 pojo时 需要加@RequestBody
     * 3、基本类型 & 数组 & MultipartFile 只要保持页面的参数名称和controller方法形参一致就不用加@RequestParam
     * 4、List 不管名字一不一样 必须加@RequestParam
     *
     * @return
     */

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (null == token) {
            return Result.error("401");
        }
        String member = jedisUtil.get(token);
        if (member == null) {
            return Result.error("401");
        }

        List<Setmeal> setmeals = setmealService.getSetmeal();
        return Result.success(MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmeals);
    }

    private static String getParmas(String url, String key) {
        String[] split = url.split("\\?");
        String[] split1 = split[1].split("&");
        for (String s : split1) {
            String[] split2 = s.split("=");
            if (split2[0].equals(key)) {
                return split2[1];
            }
        }

        return "";
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        return Result.success(MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    }
}
